package com.bomberman.environment;

import com.bomberman.items.BOOM;
import java.awt.Color;
import java.util.*;

import info.gridworld.actor.*;
import info.gridworld.grid.*;

public class Tele extends Tile
//Teles move the Man that goes in them to a different location
{
	public Man a;
	private int teleIndex;
	public Location teleLoc;
	private ArrayList<Location> teleLocs;
	private String teleType;
	
	//3 Tele types : randTele, 2Tele, cycleTele
	public Tele(BombWorld world, ArrayList<Location> TeleLocs, String tT, Location Loc)
	{
		super(world, Loc);
		teleLocs = TeleLocs;
		teleIndex = TeleLocs.size();
		teleLocs.add(tileLoc);
		teleType = tT;

		setColor(Color.darkGray);
	}
	public boolean in(Actor inA)
	{return in( ((Man) inA) );}
	
	public boolean in(Man inP)
	{
	if(a==null)
		{
		a = inP;
		inP.Loc = tileLoc;
		setColor(inP.getClr());
		occupied = true;
		return true;
		}
	else
		return false; 
	}
	
	public Man out()
	//sets Man's location to the corresponding teleLoc exit
	{
		if(a==null)
			return null;
		a.Loc = teleLoc;
		a.inTile = false;
		
		Man A = a;
		a = null; 
		setColor(Color.darkGray);
		occupied = false;
		return A;
	}
	
	public void boomed(BOOM boom)
	//if a BOOM comes in. This should be changed
	{
		a.moveTo(randAdjLoc());
		boom.got(a);
		out();
	}
	
	public void act()
	{
		teleLoc = getTeleLoc();
		
		if(occupied)
		{
			a.act();
		}
	}
	
	public void setTeleType(String tT)
	{teleType = tT;}
	
	public Location randAdjLoc()
	//Random Adjacent Location
	{
		String asdw = "ASDW";
		int count = 0;
		while(count<10000)
		{
			int n = (int)Math.floor(Math.random()*4);
			Location loc = w.asdwLoc(asdw.substring(n,n+1), getLocation());
			if(!loc.equals(getLocation()))
				return loc;
			count++;
		}
		return w.randLoc();
	}
	
	public Location getTeleLoc()
	//gets the teleLoc, varies based on teleType
	{
		if(teleType.equals("randTele"))
			return w.randLoc();
		if(teleType.equals("2Tele"))
			return teleLocs.get(teleLocs.size()-teleIndex-1);
		else
			return teleLocs.get((teleIndex+1)%teleLocs.size());
	}
}